home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int RGBattributes[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- GLX_DEPTH_SIZE, 1,
- None,
- };
-
- static int CIattributes[] = {
- GLX_DEPTH_SIZE, 1,
- None,
- };
-
- enum {
- BLACK = 0,
- RED,
- GREEN,
- YELLOW,
- BLUE,
- MAGENTA,
- CYAN,
- WHITE
- };
-
- #define COLOR_OFFSET_1 16
- #define COLOR_OFFSET_2 32
-
-
- static float rgbMap[][3] = {
- {0, 0, 0},
- {1, 0, 0},
- {0, 1, 0},
- {1, 1, 0},
- {0, 0, 1},
- {1, 0, 1},
- {0, 1, 1},
- {1, 1, 1}
- };
-
- static long W = 300, H = 300;
- static long size, mode, rgb;
- static long antiAlias = 0;
- static long stipple = 0;
- static long stencil = 0;
- static long fog = 0;
- static long smoothShade = 1;
- static long feedback = 0;
- static long ci1 = BLUE, ci2 = GREEN;
- static unsigned char stippleBits[32*4] = {
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
-
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
-
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
-
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
- };
-
-
- static void DoDisplay(void)
- {
- GLint i, n;
- GLfloat buf[20000];
-
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
-
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClearStencil(0);
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
-
- glColor3f(1.0, 0.0, 0.0);
-
- if (fog) {
- glEnable(GL_FOG);
- glFogi(GL_FOG_MODE, GL_LINEAR);
- } else {
- glDisable(GL_FOG);
- }
-
- if (smoothShade) {
- glShadeModel(GL_SMOOTH);
- } else {
- glShadeModel(GL_FLAT);
- }
-
- if (antiAlias) {
- glEnable(GL_POLYGON_SMOOTH);
- glEnable(GL_BLEND);
- glDisable(GL_DEPTH_TEST);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- } else {
- glDisable(GL_POLYGON_SMOOTH);
- glDisable(GL_BLEND);
- }
- if (stipple) {
- glPolygonStipple(stippleBits);
- glEnable(GL_POLYGON_STIPPLE);
- } else {
- glDisable(GL_POLYGON_STIPPLE);
- }
-
- if (stencil) {
- glEnable(GL_STENCIL_TEST);
- glStencilOp(GL_KEEP, GL_ZERO, GL_INCR);
- } else {
- glDisable(GL_STENCIL_TEST);
- }
-
-
- glBegin(GL_POINTS);
- for (i = 0; i < 20; i++) {
- glVertex3f(10+i*5, 95, i*0.1);
- }
- glEnd();
-
- glBegin(GL_LINE_STRIP);
- for (i = 0; i < 9; i++) {
- glVertex3f(10+i*10, 90, i*0.1);
- }
- glEnd();
-
- if (feedback) {
- glFeedbackBuffer(10000, GL_3D_COLOR, buf);
- glRenderMode(GL_FEEDBACK);
- }
- for (i = 0; i < 8; i++) {
- glBegin(GL_POLYGON);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(10+i*10, 85, i*0.1);
- glVertex3f(10+i*10, 75, i*0.1);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(20+i*10, 75, i*0.1);
- glVertex3f(20+i*10, 85, i*0.1);
- glEnd();
- }
- if (feedback) {
- n = glRenderMode(GL_RENDER);
- i = 2;
- printf("beginning %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
- i = 9;
- printf(" %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
- i = n-14;
- printf("end %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
- i = n-7;
- printf(" %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
- }
-
- glRectf(10.0, 68.0, 90.0, 73.0);
-
- glBegin(GL_LINE_LOOP);
- glColor3f(1.0, 0.0, 0.0);
- for (i = 0; i < 8; i++) {
- glVertex3f(10+i*10, 60-i, i*0.1);
- glColor3f(0.0, 0.0, 1.0);
- }
- glVertex3f(80, 65, 0.8);
- glEnd();
-
-
- glBegin(GL_TRIANGLES);
- for (i = 0; i < 9; i++) {
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(10+i*10, 45, i*0.1);
- glVertex3f(10+i*10, 35, i*0.1);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(20+i*10, 35, i*0.1);
- glVertex3f(20+i*10, 45, i*0.1);
- }
- glEnd();
-
- glBegin(GL_TRIANGLE_STRIP);
- for (i = 0; i < 9; i++) {
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(10+i*10, 30, i*0.1);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(10+i*10, 20, i*0.1);
- }
- glEnd();
-
- glPolygonMode(GL_FRONT, GL_LINE);
- glBegin(GL_TRIANGLE_STRIP);
- for (i = 0; i < 9; i++) {
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(10+i*10, 15, i*0.1);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(10+i*10, 10, i*0.1);
- }
- glEnd();
-
- glPolygonMode(GL_FRONT, GL_POINT);
- glBegin(GL_TRIANGLE_STRIP);
- for (i = 0; i < 9; i++) {
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(10+i*10, 5, i*0.1);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(10+i*10, 1, i*0.1);
- }
- glEnd();
- glPolygonMode(GL_FRONT, GL_FILL);
-
- glFlush();
- }
-
- static void Usage(void)
- {
- printf("Usage: tdepth [-c]\n");
- printf(" -c: Run in color index mode\n");
- exit(-1);
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- int main(long argc, char** argv)
- {
- XVisualInfo *vi;
- Display *dpy;
- Colormap cmap;
- Window window;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- int i;
-
- rgb = 1;
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'c':
- rgb = 0;
- break;
- default:
- Usage();
- }
- } else {
- Usage();
- }
- }
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy),
- rgb ? RGBattributes : CIattributes);
- if (!vi) {
- fprintf(stderr, "No appropriate visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- rgb ? AllocNone : AllocAll);
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- W, H,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
-
- if (!rgb) {
- XColor xc[32+2];
-
- /* Build two color ramps to use */
- unsigned long pixel;
- unsigned short red, green, blue;
- char flags; /* do_red, do_green, do_blue */
- char pad;
-
- for (i = 0; i < 16; i++) {
- xc[i].pixel = i + COLOR_OFFSET_1;
- xc[i].red = 0;
- xc[i].green = 0;
- xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
- xc[i].flags = DoRed | DoGreen | DoBlue;
-
- xc[i+16].pixel = i + COLOR_OFFSET_2;
- xc[i+16].red = 0;
- xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
- xc[i+16].blue = 0;
- xc[i+16].flags = DoRed | DoGreen | DoBlue;
- }
-
- xc[32].pixel = BLUE;
- xc[32].red = xc[32].green = 0;
- xc[32].blue = 65535;
- xc[32].flags = DoRed|DoGreen|DoBlue;
-
- xc[32].pixel = GREEN;
- xc[32].red = xc[32].blue = 0;
- xc[32].green = 65535;
- xc[32].flags = DoRed|DoGreen|DoBlue;
- XStoreColors(dpy, cmap, xc, 34);
- }
-
- mode = 0;
- size = 1;
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- W = event.xconfigure.width;
- H = event.xconfigure.height;
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_A:
- case XK_a:
- antiAlias = !antiAlias;
- if (!rgb) {
- if (antiAlias) {
- ci1 = COLOR_OFFSET_1;
- ci2 = COLOR_OFFSET_2;
- } else {
- ci1 = BLUE;
- ci2 = GREEN;
- }
- }
- needDisplay = GL_TRUE;
- break;
- case XK_E:
- case XK_e:
- feedback = !feedback;
- needDisplay = GL_TRUE;
- break;
- case XK_F:
- case XK_f:
- fog = !fog;
- needDisplay = GL_TRUE;
- break;
- case XK_S:
- case XK_s:
- stipple = !stipple;
- needDisplay = GL_TRUE;
- break;
- case XK_T:
- case XK_t:
- stencil = !stencil;
- needDisplay = GL_TRUE;
- break;
- case XK_M:
- case XK_m:
- smoothShade = !smoothShade;
- needDisplay = GL_TRUE;
- break;
- case XK_Escape:
- return 0;
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- DoDisplay();
- }
- }
- }
-